
// --------------------------------------------------
// Image Sequence
// --------------------------------------------------
var ImageSequence = function( opts ) {
	var defaults = {
			skipImages: 1,
			highresDelay: 500,			// delay between low res and high res fill
			recheckDelay: 500,			// delay before checking for current image if not present
			loadInterval: 100,			// delay to wait until loading next image if busy
			highresPattern: /vid-50/,	// regex match to replace low resolution image with high resolution
			highresReplace: 'vid'		// replace match with
		},
		sequence = {},
		currentIndex = 0,
		currentLowres,
		loadIndex = 0,
		loaded = 0,
		loadTimeout, recheckTimeout,
		timeout,
		settings = $.extend( defaults, opts );

	

	var imageOnloadHandler = function() {
		if (loaded == 0) {
			showImageAt(0);
		};
		
		if (loaded == settings.imageCount/settings.skipImages && settings.onComplete && typeof settings.onComplete === 'function') {
			// onComplete callback
			settings.onComplete();
			return false;
		}
		
		// not yet, go again
		//clearTimeout( loadTimeout );
		if (!loadTimeout) {
			loadTimeout = setTimeout( imageOnloadHandler, settings.loadInterval);
		} else {
			clearTimeout( loadTimeout );
			loadTimeout = null;
			load();
		}
		
	
		if (settings.onProgress && typeof settings.onProgress === 'function') {
			settings.onProgress();
		};

		loaded += settings.skipImages;
	};

	var load = function() {
		if (loadIndex <= settings.imageCount) {
			var image = new Image();
			image.src = settings.filesPath.replace('{index}', loadIndex);
			image.style.display = 'none';
			image.onload = imageOnloadHandler;
			settings.container.append( image );

			// force load handler even if image is cached
			if (image.complete) {
				imageOnloadHandler();
			}

			loadIndex += settings.skipImages;
		};

		/*
		for ( var i = 0; i < settings.imageCount; i=i+settings.skipImages) {
			
			var image = new Image();
			image.src = settings.filesPath.replace('{index}', i)
			image.style.display = 'none';
			image.onload = imageOnloadHandler;
			settings.container.append( image );
			
			sequence.images.push(image);

			if (image.complete) {
				imageOnloadHandler();
			}
		};*/

		return this;
	};

	

	var clearHighresImage = function() {
		if (currentLowres) settings.container.children()[ currentIndex ].src = currentLowres;
		currentLowres = null;
	};

	var loadHighresImage = function() {
		currentLowres = settings.container.children()[ currentIndex ].src;
		settings.container.children()[ currentIndex ].src = settings.container.children()[ currentIndex ].src.replace(settings.highresPattern, settings.highresReplace);

		/*currentLowres = settings.container.children()[ currentIndex ].src;
		var highresPath = settings.container.children()[ currentIndex ].src.replace(/vid-50/, 'vid');
		var image = new Image();
		image.src = highresPath;
		image.onload = showHighresImage;*/
	};

	/*
	var showHighresImage = function() {
		settings.container.children()[ currentIndex ].src = this.src;
	}*/

	var hideImageAt = function( index ) {
		var image = settings.container.children()[ index ];
		if (image) image.style.display = 'none';	
	};

	// PUBLIC
	var showImageAt = function( index ) {
		if (index == currentIndex) return false;
		
		var image = settings.container.children()[ index ];
		
		clearTimeout( timeout );

		clearHighresImage();

		if (image) {
			hideImageAt( currentIndex );
			currentIndex = index;
			image.style.display = 'block';

			timeout = setTimeout(loadHighresImage, settings.highresDelay);
		} else {
			clearTimeout(recheckTimeout);
			recheckTimeout = setTimeout(showImageAt, settings.recheckDelay, index);
		}
		
	};

	return {
		load: load,
		showImageAt: showImageAt,
		imageCount: settings.imageCount,
		skipImages: settings.skipImages
	}
}




